package com.beeantec.jnrerc.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.dto.CourseCateGoryEditDTO;
import com.beeantec.jnrerc.dto.CourseCateGoryReqDTO;
import com.beeantec.jnrerc.dto.CourseCategoryDTO;
import com.beeantec.jnrerc.dto.response.CourseCategoryListDTO;
import com.beeantec.jnrerc.entity.CourseCategory;
import com.beeantec.jnrerc.modules.course.service.CourseCategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.swagger.annotations.ApiOperation;

/**
 * 课程分类
 *
 * @author Anthony
 */
@RestController
@RequestMapping(value = "/api/coursecategory")
@Api(value = "课程分类", tags = "课程分类")
public class CourseCategoryController extends BaseController {

	@Autowired
	private CourseCategoryService courseCategoryService;

	/**
	 * 普通课程分类列表接口
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "课程分类列表接口", notes = "课程分类列表")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ApiRest<CourseCategoryListDTO> list() {
		return success(courseCategoryService.listAll());
	}


	/**
	 * 添加课程分类接口
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "添加课程分类", notes = "添加课程分类")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ApiRest<Boolean> add(@RequestBody CourseCateGoryReqDTO courseCateGoryReqDTO) {
		CourseCategory courseCategory = new CourseCategory();
		BeanUtils.copyProperties(courseCateGoryReqDTO,courseCategory);
		return success(courseCategoryService.save(courseCategory));
	}

	/**
	 * 编辑课程分类
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "编辑课程分类", notes = "编辑课程分类")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ApiRest<Boolean> edit(@RequestBody CourseCateGoryEditDTO courseCateGoryReqDTO) {
		CourseCategory courseCategory = new CourseCategory();
		BeanUtils.copyProperties(courseCateGoryReqDTO,courseCategory);
		return success(courseCategoryService.saveOrUpdate(courseCategory));
	}


	/**
	 * 删除课程分类
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "删除课程分类", notes = "删除课程分类")
	@RequestMapping(value = "/remove", method = RequestMethod.GET)
	public ApiRest<Boolean> remove(@RequestParam("categoryId") Long id) {
		return success(courseCategoryService.removeById(id));
	}



	/**
	 * 获取课程分类详情
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "获取课程分类详情", notes = "获取课程分类详情")
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public ApiRest<CourseCateGoryEditDTO> detail(@RequestParam("categoryId") Long id) {
		CourseCategory courseCategory = courseCategoryService.getById(id);
		CourseCateGoryEditDTO courseCateGoryEditDTO = new CourseCateGoryEditDTO();
		BeanUtils.copyProperties(courseCategory,courseCateGoryEditDTO);
		return success(courseCateGoryEditDTO);
	}




	/**
	 * 普通课程分类列表接口
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "获取项目", notes = "获取项目")
	@RequestMapping(value = "/project/list", method = RequestMethod.POST)
	public ApiRest<CourseCategoryListDTO> listProject() {
		return success(courseCategoryService.listAllProject());
	}


	/**
	 * 置顶项目
	 *
	 * @author anthony
	 */
	@ApiOperation(value = "置顶项目", notes = "置顶项目")
	@RequestMapping(value = "/top", method = RequestMethod.GET)
	public ApiRest<Boolean> top(@RequestParam("categoryId") Long id) {
		CourseCategory courseCategory = courseCategoryService.getById(id);

		CourseCategory max = courseCategoryService.getOne(new LambdaQueryWrapper<CourseCategory>().orderByDesc(CourseCategory::getSort).last("limit 1"));
		courseCategory.setSort(max.getSort()+1);
		return success(courseCategoryService.saveOrUpdate(courseCategory));
	}

}
